package com.genfu.reform.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.genfu.reform.model.RoleInfo;
import com.genfu.reform.model.UserInfo;
import com.genfu.reform.service.GenfuCommonService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@Results({
		@Result(name = "createOver", location = "createOver.jsp"),
		@Result(name = "success", type = "redirectAction", params = {
				"actionName", "user-info" }) })
public class UserInfoController extends ValidationAwareSupport implements
		ModelDriven<Object>, Validateable, SessionAware, ServletRequestAware,
		ServletResponseAware, ParameterAware, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserInfo model = new UserInfo();// 姝ゅ蹇呴』瀹炰緥鍖栦竴涓璞�鐢ㄦ潵鏂板缓瀵硅薄
	private Long id;
	private Collection<UserInfo> list;
	private GenfuCommonService genfuCommonService;
	// private List<RoleInfo> roles;
	private Map<String, Object> session;
	private Map<String, String[]> parameters;
	private String roleIds;
	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	private boolean verifyingOperates;

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	// public List<RoleInfo> getRoles() {
	// return roles;
	// }
	//
	// public void setRoles(List<RoleInfo> roles) {
	// this.roles = roles;
	// }

	public UserInfoController(GenfuCommonService theService) {
		this.genfuCommonService = theService;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		System.out.println("Controller...setServletResponse..." + arg0);

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {

	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		System.out.println("Controller...setSession...");
		this.session = arg0;
	}

	@Override
	public void validate() {
		System.out.println("Controller...validate...");

	}

	@Override
	public Object getModel() {
		System.out.println("Controller...getModel...");
		if (model != null) {
			System.out
					.println("Controller...getModel..." + model.getUserCode());
		}
		return (list != null ? list : model);
	}

	public void setModel(UserInfo model) {
		System.out.println("Controller...setModel...");
		this.model = model;
	}

	public HttpHeaders show() {
		System.out.println("Controller...show...");
		// if (this.id != 0 && this.model.getId() == null) {
		// this.model = eventService.get(this.id);
		// }
		return new DefaultHttpHeaders("show");
	}

	public void prepareIndex() throws Exception {
		System.out.println("Controller...prepareindex...");
		verifyingOperates = genfuCommonService.verifyingOperates(parameters,
				session);
	}

	public HttpHeaders index() {
		if (verifyingOperates && this.parameters.containsKey("statusCode")) {
			list = genfuCommonService.searchList(UserInfo.class, parameters);
			return new DefaultHttpHeaders("result").disableCaching();
		}
		return new DefaultHttpHeaders("index").disableCaching();
	}

	// @Action("search")
	public HttpHeaders search() {
		System.out.println("Controller...search...");
		list = genfuCommonService.findAll();
		return new DefaultHttpHeaders("search").disableCaching();
	}

	public String update() {
		if (null != roleIds && roleIds.length() > 0) {
			model.setROLE_INFO(genfuCommonService.searchList(
					"from RoleInfo where ROLE_ID in(" + roleIds + ") ", null,
					RoleInfo.class));
		}
		genfuCommonService.update(model);
		addActionMessage("Object updated successfully");
		return "success";
	}

	public void setId(Long id) {
		System.out.println("Controller...setId..." + id);
		if (id != null) {
			this.model = (UserInfo) genfuCommonService.find(id, UserInfo.class);
		}
		this.id = id;
	}

	public String edit() {
		System.out.println("Controller...edit...");

		return "edit";
	}

	public String editNew() {
		System.out.println("Controller...editNew...");
		return "editNew";
	}

	public String create() {
		if (null != roleIds && roleIds.length() > 0) {
			model.setROLE_INFO(genfuCommonService.searchList(
					"from RoleInfo where ROLE_ID in(" + roleIds + ") ", null,
					RoleInfo.class));
		}
		genfuCommonService.save(model);
		addActionMessage("New Object created successfully");
		Path target = FileSystems.getDefault().getPath("userImages");

		// return new DefaultHttpHeaders("close");
		// return new
		// DefaultHttpHeaders("success").setLocationId(model.getId());
		try {
			Files.copy(userImage.toPath(), target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "createOver";
	}

	public String deleteConfirm() {
		System.out.println("Controller...deleteConfirm...");
		return "deleteConfirm";
	}

	public String destroy() {
		System.out.println("Controller...destroy...");
		genfuCommonService.remove(model);
		addActionMessage("Object removed successfully");
		return "success";
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void setParameters(Map<String, String[]> arg0) {
		this.parameters = arg0;
	}
}
